/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.persistence.memory.criteria.order;

import java.util.List;

import com.genia.toolbox.persistence.basis.criteria.BasicCriteriaResult;
import com.genia.toolbox.persistence.criteria.order.Order;
import com.genia.toolbox.persistence.exception.AggregateException;
import com.genia.toolbox.persistence.exception.FieldDefinitionException;
import com.genia.toolbox.persistence.memory.criteria.field.AbstractMemoryField;

/**
 * the base of the implementation of <code>Order</code> for the memory
 * implementation of the persistence API.
 */
public class MemoryOrder
    implements Order
{

  /**
   * Wether this order is ascendent of descendent.
   */
  public enum Direction
  {
    /**
     * constant for the ascendent order.
     */
    ASC(1),
    /**
     * constant for the descendent order.
     */
    DESC(-1);

    /**
     * Constructor.
     * 
     * @param direction
     *          wether this order is ascendent or descendent. 1 is ascendent and
     *          -1 is descendent.
     */
    Direction(final int direction)
    {
      this.direction = direction;
    }

    /**
     * wether this order is ascendent or descendent. 1 is ascendent and -1 is
     * descendent.
     */
    private int direction;



    /**
     * getter for the direction property.
     * 
     * @return the direction
     */
    public int getDirection()
    {
      return direction;
    }

  }

  /**
   * The field we order with.
   */
  private final transient AbstractMemoryField field;

  /**
   * The direction of this sort.
   */
  private final transient Direction direction;



  /**
   * Constructor.
   * 
   * @param direction
   *          the direction of this sort
   * @param field
   *          the field we order with
   */
  public MemoryOrder(final Direction direction, final AbstractMemoryField field)
  {
    this.direction = direction;
    this.field = field;
  }



  /**
   * The comparator function.
   * 
   * @param line1
   *          the first line of result
   * @param line2
   *          the second line of result
   * @return 1 if line1 is greater than line2, -1 if line1 if lesser than line2
   *         and 0 is those 2 line are not ordered
   * @throws FieldDefinitionException
   *           when there is an error in the definition of the field
   * @throws AggregateException
   *           when there is an error with an aggregate definition
   */
  @SuppressWarnings("unchecked")
  public int compare(final List<BasicCriteriaResult<?>> line1, final List<BasicCriteriaResult<?>> line2)
      throws FieldDefinitionException, AggregateException
  {
    final Object value1 = field.getValue(line1);
    final Object value2 = field.getValue(line2);

    if (value1 == null) {
      if (value2 == null) {
        return 0;
      }
      return -1 * direction.getDirection();
    }
    if (value2 == null) {
      return direction.getDirection();
    }

    if (value1 instanceof Comparable) {
      return direction.getDirection() * ((Comparable<Object>) value1).compareTo(value2);
    }
    return direction.getDirection() * value1.toString().compareTo(value2.toString());

  }

}
